﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class WeaponSlotObject : PartObject
{
    public WeaponSlot weaponSlot { 
        set {
            if (value == null)
            {
                return;
            }
            m_WeaponSlot = value;
            m_WeaponSlot.onFire = OnFire;
        }
        get {
            return m_WeaponSlot; }
    }
    public Ship ship { set { m_Ship = value; } get { return m_Ship; } }
    public int typeID { get; set; }
    public Ship m_Ship;
    WeaponSlot m_WeaponSlot;
    WeaponObject m_WeaponObj;
    public void Init()
    {
        if (m_WeaponObj != null)
        {
            Destroy(m_WeaponObj.gameObject);
        }
        m_WeaponObj = null;
        if (m_WeaponSlot.weapon == null)
            return;
        GameObject weaponObj = new GameObject();
        switch(m_WeaponSlot.weapon.m_Type)
        {
            case Item.ItemType.ARTILLARY:
                m_WeaponObj = weaponObj.AddComponent<WeaponArtilleryObject>();
                break;
        }
        if (m_WeaponObj == null)
            return;
        gameObject.SetActive(true);
        m_WeaponObj.weapon = m_WeaponSlot.weapon;
        m_WeaponObj.ship = m_Ship;
        m_WeaponObj.Init(this);
        m_WeaponObj.transform.SetParent(transform);
        m_WeaponObj.transform.localPosition = new Vector3(0,0,0);
    }

    public void SetWeapon(Weapon weapon)
    {
        if(m_WeaponSlot == null)
        {
            return;
        }
        m_WeaponSlot.weapon = weapon;
        Init();
    }
    public void OnFire()
    {
        if (m_WeaponObj)
        {
            m_WeaponObj.Fire();
        }
    }

    public void Arm(Vector3 targetPosition)
    {
        if(m_WeaponObj)
            m_WeaponObj.Arm(targetPosition);
    }
}